/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = syncmodule_data_format.c                           */
/*  NOTE      =                                                    */
/*  DATE      = 2015/03/11 by guojq                                */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <syncmodule/syncmodule_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frreport_comm.h"
#include "../../../src/module/syncmodule/config.h"

typedef struct __st_L7session_syncmodule {
    unsigned long ul_seq;                              /* 序列号，1次req和response时，使用同一个序列号 */
    Frlist_header *p_lst_req_tmp;                       /* req信息列表 */
    Frlist_header *p_lst_comm_tmp;                      /* 共通信息 */
    //char arr_session_id[APM_UUID_SIZE];              /* session ID, UUID */
   L4_transaction st_l4_trans;                         /* 数据 */

}L7session_syncmodule;

extern int syncmodule_data_format(Conntrack *p_st_conn
                        , Frhash_header *p_st_hash_report
                        , L4_transaction *p_st_l4_private
                        , unsigned long ul_seq
                        , L7session_syncmodule *p_st_private);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    L4_transaction st_l4;
    Frhash_header st_hash_report;
    L7session_syncmodule *p_st_private=NULL;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_private = (L7session_syncmodule *)frmalloc(sizeof(L7session_syncmodule));
    memset(p_st_private, 0x00, sizeof(L7session_syncmodule));

    p_st_conn->ip_proto = IPPROTO_TCP;
    p_st_conn->src_ip = iptools_parse_ipaddr("172.16.0.1");
    p_st_conn->src_port = 80;
    p_st_conn->dst_ip = iptools_parse_ipaddr("172.16.0.15");
    p_st_conn->dst_port = 8000;

    frhash2_create(&st_hash_report, 10);
    memset(&st_l4, 0x00, sizeof(L4_transaction));

    st_l4.st_request_time_first_byte.tv_sec = 1417060359;
    st_l4.st_request_time_start.tv_sec = 1417060359;
    st_l4.st_request_time.tv_sec = 1417060359;
    st_l4.l_request_bytes = 100;

    st_l4.st_response_time_first_byte.tv_sec = 1417060360;
    st_l4.st_response_time_first_byte.tv_usec = 1000;
    st_l4.l_response_bytes = 2000;

    i_ret = syncmodule_data_format(NULL, &st_hash_report, &st_l4, 1, p_st_private);

    frhash2_free(&st_hash_report, NULL);
    frfree(p_st_private);
    frfree(p_st_conn);

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);
    return;
}

static void case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    L4_transaction st_l4;
    Frhash_header st_hash_report;
    L7session_syncmodule *p_st_private=NULL;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_private = (L7session_syncmodule *)frmalloc(sizeof(L7session_syncmodule));
    memset(p_st_private, 0x00, sizeof(L7session_syncmodule));

    p_st_conn->ip_proto = IPPROTO_TCP;
    p_st_conn->src_ip = iptools_parse_ipaddr("172.16.0.1");
    p_st_conn->src_port = 80;
    p_st_conn->dst_ip = iptools_parse_ipaddr("172.16.0.15");
    p_st_conn->dst_port = 8000;

    frhash2_create(&st_hash_report, 10);
    memset(&st_l4, 0x00, sizeof(L4_transaction));

    st_l4.st_request_time_first_byte.tv_sec = 1417060359;
    st_l4.st_request_time_start.tv_sec = 1417060359;
    st_l4.st_request_time.tv_sec = 1417060359;
    st_l4.l_request_bytes = 100;

    st_l4.st_response_time_first_byte.tv_sec = 1417060360;
    st_l4.st_response_time_first_byte.tv_usec = 1000;
    st_l4.l_response_bytes = 2000;

    i_ret = syncmodule_data_format(p_st_conn, NULL, &st_l4, 1,p_st_private);

    frhash2_free(&st_hash_report, NULL);
    frfree(p_st_private);
    frfree(p_st_conn);

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);
    return;
}

static void case3(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    L4_transaction st_l4;
    Frhash_header st_hash_report;
    L7session_syncmodule *p_st_private=NULL;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_private = (L7session_syncmodule *)frmalloc(sizeof(L7session_syncmodule));
    memset(p_st_private, 0x00, sizeof(L7session_syncmodule));

    p_st_conn->ip_proto = IPPROTO_TCP;
    p_st_conn->src_ip = iptools_parse_ipaddr("172.16.0.1");
    p_st_conn->src_port = 80;
    p_st_conn->dst_ip = iptools_parse_ipaddr("172.16.0.15");
    p_st_conn->dst_port = 8000;

    frhash2_create(&st_hash_report, 10);
    memset(&st_l4, 0x00, sizeof(L4_transaction));

    st_l4.st_request_time_first_byte.tv_sec = 1417060359;
    st_l4.st_request_time_start.tv_sec = 1417060359;
    st_l4.st_request_time.tv_sec = 1417060359;
    st_l4.l_request_bytes = 100;

    st_l4.st_response_time_first_byte.tv_sec = 1417060360;
    st_l4.st_response_time_first_byte.tv_usec = 1000;
    st_l4.l_response_bytes = 2000;

    syncmodule_data_format(p_st_conn, &st_hash_report, &st_l4, 1,p_st_private);

    frhash2_free(&st_hash_report, NULL);
    frfree(p_st_private);
    frfree(p_st_conn);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    return;
}

int add_suite_syncmodule_data_format(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" syncmodule_data_format()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
